! function(global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.imconfig_touch = factory());
}(this, function() {
    function myTouch(el, obj) {
        if (typeof el !== 'object' || el.nodeType !== 1) {
            throw new Error('this touch lib need a right el.');
        }
        el.addEventListener('touchstart', touchStart, false);
        el.addEventListener('touchmove', touchMove, false);
        el.addEventListener('touchend', touchEnd, false);

        el.is_imconfig_touch = true;

        this.el = el;
        this.touch = false;
        this.time = 0;
        this.type = false;
        this.poss = false;
        this.x = (obj && obj.x) || 0;
        this.y = (obj && obj.y) || 0;
        this.z = (obj && obj.z) || 0;
        this.r = (obj && obj.r) || 0;
        this.s = (obj && obj.s) || 1;
        this.minS = (obj && obj.minS) || 0.001;

        this.tx = 0;
        this.ty = 0;
        this.tz = 0;
        this.tr = 0;
        this.ts = 1;
        this.begin = [];

        this.tempPos = {};
        var touch = this;

        function touchStart(e) {
            e.preventDefault();
            e.stopPropagation();
            touch.touch = true;
            var poss = e.touches;
            if (poss.length === 1) {
                touch.begin[0] = {
                    clientX: poss[0].clientX,
                    clientY: poss[0].clientY
                };
            } else if (poss.length === 2) {
                touch.begin[0] = {
                    clientX: poss[0].clientX,
                    clientY: poss[0].clientY
                };
                touch.begin[1] = {
                    clientX: poss[1].clientX,
                    clientY: poss[1].clientY
                };
                touch.tempPos[0] = {
                    clientX: poss[0].clientX,
                    clientY: poss[0].clientY
                };
                touch.tempPos[1] = {
                    clientX: poss[1].clientX,
                    clientY: poss[1].clientY
                };
            }
        }

        function touchMove(e) {
            var poss = e.touches;
            e.preventDefault();
            e.stopPropagation();
            if (touch.touch !== true) {
                return;
            }
            if (poss.length === 1) {
                var posB = touch.begin[0];
                var posE = poss[0];
                touch.tx = posE.clientX - posB.clientX;
                touch.ty = posE.clientY - posB.clientY;
            } else if (poss.length === 2) {
                var b1 = touch.begin[0];
                var b2 = touch.begin[1];
                var n1 = poss[0];
                var n2 = poss[1];
                //防止抖动
                if (touch.getLineWidth(touch.tempPos[0], n1) > touch.getLineWidth(touch.tempPos[0], n2)) {
                    n1 = n2;
                    n2 = poss[0];
                }
                touch.tempPos[0] = n1;
                touch.tempPos[1] = n2;
                var deg1 = touch.getDeg(b1, b2);
                var deg2 = touch.getDeg(n1, n2);
                touch.tr = deg2 - deg1;

                var lin1 = touch.getLineWidth(b1, b2);
                var lin2 = touch.getLineWidth(n1, n2);
                touch.ts = lin2 / lin1;
            }
            touch.refresh();
        }

        function touchEnd(e) {
            var poss = e.touches;
            e.preventDefault();
            e.stopPropagation();
            if (touch.touch !== true) {
                return;
            }

            touch.touch = false;
            touch.refresh('end');
        }
    }

    myTouch.prototype.getLineWidth = function(b, e) {
        var dx = e.clientX - b.clientX;
        var dy = e.clientY - b.clientY;
        return Math.sqrt(dx * dx + dy * dy);
    };
    myTouch.prototype.getDeg = function(b, e) {
        var dx = e.clientX - b.clientX;
        var dy = e.clientY - b.clientY;
        //var dd = Math.sqrt(dx * dx + dy * dy);
        var tan = dy / dx;
        res = Math.atan(tan) / Math.PI * 180;
        if(dx < 0){
            res -= 180;
        }
        return res;
    };
    myTouch.prototype.refresh = function(type) {
        if (type === 'end') {
            this.x += this.tx;
            this.y += this.ty;
            this.z += this.tz;
            this.r = (this.r + this.tr) % 360;
            this.s *= this.ts;

            this.tr = 0;
            this.tx = 0;
            this.ty = 0;
            this.tz = 0;
            this.ts = 1;
        }
        var s = Math.max(this.s * this.ts, this.minS);
        var style = [];
        style.push(['translate3d(', this.x + this.tx, 'px, ', this.y + this.ty, 'px, ', this.z + this.tz, 'px)'].join(''));
        style.push(['rotateZ(', this.r + this.tr, 'deg)'].join(''));
        style.push(['scale(' + s + ')'].join(''));
        this.el.style.transform = style.join(' ');
    };

    return myTouch;
});